<?PHP
session_start();

require("../../config.php");
require("../../db/opendb.php");

$listing_id = $_GET['listing_id'];

$user_id = $_SESSION['id'];
if(isset($_GET['wp_user']) && $_GET['wp_user']) {
	$sql = "SELECT * FROM Users WHERE Username='{$_GET['wp_user']}'";
	$user_rs = mysql_query($sql) or die ("SQL Error".mysql_error());
	$user = mysql_fetch_assoc($user_rs);
	$user_id = $user['ID'];
}

$lquery = "select * from PROPERTIES p inner join Prop_Owner_Con c on p.OID = c.Prop_OID where p.OID='".$listing_id."' and c.Owner = '".$user_id."'";
$lresult = mysql_query($lquery) or die ("SQL Error".mysql_error());
if(mysql_num_rows($lresult)>0) {
	$row = mysql_fetch_assoc($lresult);
	//	echo "<pre>";print_r($row);exit;
	$sql = "SELECT * FROM Users WHERE ID='{$row['Owner']}'";
	$agent_rs = mysql_query($sql) or die ("SQL Error".mysql_error());
	$agent = mysql_fetch_assoc($agent_rs);
	//		echo "<pre>";print_r($agent);exit;
}
else {
	echo "Listing not found";exit;
}

$mquery2 = "select * from Listing_Images where Listing_ID = '".$listing_id."' and Type = 'IMAGE' ORDER BY sort_order LIMIT 7";

$mresult2 = mysql_query($mquery2) or die ("SQL Error".mysql_error());
$images = '';
$main_image = '';
if(mysql_num_rows($mresult2)>0) {
	$mrow2 = mysql_fetch_assoc($mresult2);
	$main_image = $mrow2['Server_Path'].$mrow2['Path'];
	while($mrow2 = mysql_fetch_assoc($mresult2))
	{
		$images .= "<img src=\"{$mrow2['Server_Path']}{$mrow2['Path']}\" width='100'>";
	}
}

$agent_info = array();
if($agent['First_Name'] || $agent['Last_Name']) $agent_info[] = $agent['First_Name']." ".$agent['Last_Name'];
if($agent['Business_Name']) $agent_info[] = $agent['Business_Name'];
if($agent['Phone']) $agent_info[] = "Phone: {$agent['Phone']}";
if($agent['Email']) $agent_info[] = $agent['Email'];
$url = '';
if($agent['Website']) {
	if(stripos($agent['Website'], "http://")===false) {
		$agent_info[] = "http://".$agent['Website'];
	}
	else {
		$agent_info[] = $agent['Website'];
	}
}
$agent_info_str = implode("<br/>", $agent_info);
//echo "<pre>";print_r($row);exit;
$desc_length = 1500;
if(strlen($row['REMARKS'])>$desc_length) $row['REMARKS'] = substr($row['REMARKS'], 0, $desc_length);

$full_address = $row['STREETNUM']." ".$row['STREETDIR']." ".$row['STREETNAME']." ".$row['STREETSUFFIX']."".$unit.", ".$row['CITY']." ".$row['STATE']." ".$row['ZIPCODE'];

$output = "
<style>
table td{font-size:20px;}
img {margin:3px;}
</style>
<table width=\"800\">
	<tr>
		<td colspan=\"2\" style=\"font-size:35px;\">$full_address</td>
	</tr>
	<tr>
		<td colspan=\"2\" style=\"font-size:25px;\">
		{$row['TITLE']}
		<hr>
		</td>
	</tr>
	<tr>
		<td width=\"50%\" valign=\"top\">
			<div><img src=\"$main_image\" width='400'></div>
			<div>
			$images
			</div>
		</td>
		<td width=\"50%\" valign=\"top\">
			<table width=\"100%\">
				<tr>
					<td colspan=\"2\" style=\"font-size:22px;\">Price: $".number_format($row['LISTPRICE'])."</td>
				</tr>
				<tr>
					<td colspan=\"2\">Property Details</td>
				</tr>
				<tr>
					<td>Beds:</td>
					<td>{$row['BEDS']}</td>
				</tr>
				<tr>
					<td>Baths:</td>
					<td>{$row['BATHSTOTAL']}</td>
				</tr>
				<tr>
					<td>SQFT:</td>
					<td>{$row['SQFTTOTAL']}</td>
				</tr>
				<tr>
					<td>Parking:</td>
					<td>{$row['PARKINGCOVERED']}</td>
				</tr>
				<tr>
					<td>Year Built:</td>
					<td>{$row['YEARBUILT']}</td>
				</tr>
				<tr>
					<td>MLS Number:</td>
					<td>{$row['MLSNUM']}</td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td colspan=\"2\"><br>
		{$row['REMARKS']}
		</td>
	</tr>
	<tr>
		<td colspan=\"2\"><br>
			<table width=\"100%\">
				<tr>
					<td width=\"200\"><img src=\"/user_images/{$agent['Image']}\"></td>
					<td width=\"200\">
					$agent_info_str
					</td>
					<td><img src=\"/user_images/t_{$agent['Company_Logo']}\"></td>
					<td><img src='https://chart.googleapis.com/chart?chs=250x100&cht=qr&chl=http://".$_SERVER['HTTP_HOST']."/vt/$listing_id'></td>
				</tr>
			</table>
		</td>
	</tr>
</table>";
//echo $output;exit;
include("mpdf/mpdf.php");
$mpdf=new mPDF('s');
$mpdf->SetDisplayMode('fullpage');
$mpdf->simpleTables = true;
$mpdf->WriteHTML($output);   // Separate Paragraphs  defined by font
//$mpdf->Output();
$content = $mpdf->Output("", "S");

header("Content-type:application/pdf");
// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename=flyer.pdf");
echo $content;
?>